//
//  ESHUDView.m
//  Edusoho
//
//  Created by Edusoho on 14-7-18.
//  Copyright (c) 2014年 Kuozhi Network Technology. All rights reserved.
//

#import "ESHUDView.h"
#import "SVProgressHUD.h"

@implementation ESHUDView

+ (instancetype)sharedInstance
{
    static ESHUDView *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [ESHUDView new];
    });
    
    return _sharedInstance;
}

+ (void)showLoading
{
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
}

+ (void)showWithStatus:(NSString *)string
{
    [SVProgressHUD showWithStatus:string maskType:SVProgressHUDMaskTypeClear];
}

+ (void)dismiss
{
    [SVProgressHUD dismiss];
}

- (void)showSuccessMessage:(NSString *)string dismissBlock:(ESHUDViewDismissBlock)block
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleNotification:)
                                                 name:SVProgressHUDDidDisappearNotification
                                               object:nil];
    [SVProgressHUD showSuccessWithStatus:string maskType:SVProgressHUDMaskTypeBlack];
    if (block) {
        _dismissBlock = block;
    }
}

- (void)showErrorMessage:(NSString *)string dismissBlock:(ESHUDViewDismissBlock)block
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleNotification:)
                                                 name:SVProgressHUDDidDisappearNotification
                                               object:nil];
    [SVProgressHUD showErrorWithStatus:string maskType:SVProgressHUDMaskTypeBlack];
    if (block) {
        _dismissBlock = block;
    }
}

- (void)handleNotification:(NSNotification *)aNotification
{
    if (_dismissBlock) {
        _dismissBlock();
    }
    _dismissBlock = nil;
}

@end
